package server

import (
	"errors"
	"log"
	"product-module/product-rpc/initiate/mysql"
	"product-module/product-rpc/proto/good"
)

func GoodCreate(in *good.GoodCreateReq) (*good.GoodCreateResp, error) {
	m := mysql.Good{
		GoodName:     in.GoodName,
		Image:        in.Image,
		Video:        in.Video,
		GoodCode:     in.GoodCode,
		GoodType:     in.GoodType,
		SellingPoint: in.SellingPoint,
		Inventory:    in.Inventory,
		SalesVolume:  in.SalesVolume,
		PageView:     in.PageView,
		Status:       in.Status,
	}
	err := m.Create()
	if err != nil {
		log.Println("商品添加失败")
		return nil, errors.New("商品添加失败")
	}
	return &good.GoodCreateResp{
		Greet: "商品添加成功",
		Code:  200,
	}, nil
}

func Sttribute(in *good.SttributeReq) (*good.SttributeResp, error) {
	s := mysql.Sttribute{
		GoodId:     in.GoodId,
		GoodColour: in.GoodColour,
		SetMenu:    in.SetMenu,
	}
	err := s.Create()
	if err != nil {
		log.Println("商品属性添加失败")
		return nil, errors.New("商品属性添加失败")
	}
	return &good.SttributeResp{
		Greet: "商品属性添加成功",
		Code:  200,
	}, nil
}

func Sales(in *good.SalesReq) (*good.SalesResp, error) {
	s := mysql.Sales{
		Benefit:  in.Benefit,
		Merchant: in.Merchant,
		StarTime: in.StarTime,
	}
	err := s.Create()
	if err != nil {
		log.Println("售后填入失败")
		return nil, errors.New("售后填入失败")
	}
	return &good.SalesResp{
		Greet: "售后填入成功",
		Code:  200,
	}, nil
}

func GoodList(in *good.GoodListReq) (*good.GoodListResp, error) {
	m := mysql.Good{}
	li, err := m.List()
	if err != nil {
		log.Println("展示失败")
		return nil, errors.New("展示失败")
	}
	list := []*good.GoodItem{}
	for _, g := range li {
		lists := &good.GoodItem{
			GoodName:     g.GoodName,
			Image:        g.Image,
			Video:        g.Video,
			GoodCode:     g.GoodCode,
			SellingPoint: g.SellingPoint,
			Inventory:    g.Inventory,
			SalesVolume:  g.SalesVolume,
			GoodType:     g.GoodType,
			PageView:     g.PageView,
			Status:       g.Status,
		}
		list = append(list, lists)
	}
	return &good.GoodListResp{List: list}, nil
}

func GoodFind(in *good.GoodFindReq) (*good.GoodListResp, error) {
	m := mysql.Good{}
	find, err := m.Find(in.GoodId)
	if err != nil {
		log.Println("查询失败")
		return nil, errors.New("查询失败")
	}
	list := []*good.GoodItem{}
	for _, g := range find {
		lists := &good.GoodItem{
			GoodName:     g.GoodName,
			Image:        g.Image,
			Video:        g.Video,
			GoodCode:     g.GoodCode,
			SellingPoint: g.SellingPoint,
			Inventory:    g.Inventory,
			SalesVolume:  g.SalesVolume,
			GoodType:     g.GoodType,
			PageView:     g.PageView,
			Status:       g.Status,
		}
		list = append(list, lists)
	}
	return &good.GoodListResp{List: list}, nil
}
